package com.leo.chapter14;

/**
 * 可重新关闭的阀门类
 * @author xuexiaolei
 * @version 2017年10月20日
 */
public class ThreadGate {
    private boolean isOpen;
    private int generation;
    public synchronized void close(){
        isOpen = false;
    }
    public synchronized void open(){
        ++generation;
        isOpen=true;
        notify();
    }
    public synchronized void await() throws InterruptedException{
        int arrivalGeneration = generation;
        while (!isOpen && arrivalGeneration == generation){
            wait();
        }
    }
}
